package com.kelvin.retrofitrxdemo;


import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by MCK on 2017/4/20.
 */

public class ServiceGenerator {
    private static final String BASE_URL = "https://api.github.com/";

    private static Retrofit retrofit;

    private static Retrofit.Builder builder =
            new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create());


    private static OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor
            (new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)).build();

    public static <S> S createService(
            Class<S> serviceClass) {

            builder.client(okHttpClient);
            retrofit = builder.build();
        return retrofit.create(serviceClass);
    }

//    private static OkHttpClient.Builder addInterceptor(OkHttpClient okHttpClient) {
//        httpClient.addInterceptor(new Interceptor() {
//            @Override
//            public Response intercept(Chain chain) throws IOException {
//                Request original = chain.request();
//                //通过拦截器给okhttpUtil加上头
//                Request.Builder requestBuilder = original.newBuilder()
//                        .addHeader("Cache-Control", "no-cache")
//                        .addHeader("Cache-Control", "no-store");
//                Request request = requestBuilder.build();
//                return chain.proceed(request);
//            }
//        });
//        return httpClient;
//    }
}
